<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/4/18
 * Time: 10:34
 */

class Login extends M_Controller
{
    public function index(){
//        echo md5('123456');exit;
        $data = $error = $success = '';
        $USER = $this->get_cache('user');
        if(IS_POST){
//            $data = $this->input->post('data',true);
            $phone = $this->input->get_post('phone');
            $pass = $this->input->get_post('pass');
            $back_url = dr_user_url('home/index');
            if($USER['setting']['logincode'] && !$this->check_captcha('code')){
                $error = fc_lang('验证码不正确');
            }else if(!$phone || !$pass){
                $error = fc_lang('手机号或是密码输入错误，请重新输入');
                //$error = fc_lang('<script>手机号或是密码输入,错误请重新输入.</script>');
            }else{
                //判断是否可以登录
                $this->load->model('user_model');
                $code = $this->user_model->login($phone, $pass, $data['auto'] ? 864000000 : $USER['setting']['loginexpire']);
                if($code == 1){
                    // 登录成功
                    $data['id'] = $this->uid;
                    $this->hooks->call_hook('user_login', $data); // 登录成功挂钩点
                    $success = $code;
                }else if($code == -1){
                    $error = fc_lang('用户不存在');
                }else if($code == -2){
                    $error = fc_lang('您的帐号或密码有误，请重新输入');
                }else if($code == -3){
                    $error = fc_lang('不是手机号码');
                }
            }
            if(IS_AJAX){
                $error && exit(dr_json(0, $error));
                $success && exit(json_encode(array(
                    'status' => 1,
                    'backurl' => $back_url,
                    'isdebug' => $_GET['debug'],
//                    'syncurl' => dr_member_sync_url($success)
                )));
            }

            $success && $this->user_msg(fc_lang('登录成功').$success, $back_url, 1, $_GET['debug'] ? 99999 : 3);
            exit('error');
        }else{
            $back_url = $this->input->get('back')?$this->input->get('back'):(isset($_SERVER['HTTP_REFERER']) ? (strpos($_SERVER['HTTP_REFERER'], 'login') !== false ? '' : $_SERVER['HTTP_REFERER']) : '');
        }
        $this->template->assign(array(
            'data'=>$data,
            'code' => $USER['setting']['logincode'],
            'back_url' =>$back_url,
            'meta_title'=>fc_lang('用户登录'),
            'result_error'=> $error
        ));
        $this->template->display('../gw/login.html');
    }
    public function out(){
        if(IS_AJAX){
            exit(json_encode(array(
                'backurl' => SITE_URL,
                'syncurl' => dr_member_sync_url($this->member_model->logout()))));
        }else{
            delete_cookie('user_id');
            delete_cookie("name");
            delete_cookie("mobile");
            $this->load->model('user_model');
            $this->member_msg(fc_lang('您已经成功退出了'), SITE_URL, 0, 0);
        }
    }


    public function poup_login(){
//        echo md5('123456');exit;
        $data = $error = $success = '';
        $USER = $this->get_cache('user');
        if(IS_POST){
//            $data = $this->input->post('data',true);
            $phone = $this->input->get_post('phone');
            $pass = $this->input->get_post('pass');
            $back_url = dr_user_url('home/index');
            if($USER['setting']['logincode'] && !$this->check_captcha('code')){
                $error = fc_lang('验证码不正确');
            }else if(!$phone || !$pass){
                $error = fc_lang('手机号或是密码输入错误，请重新输入');
                //$error = fc_lang('<script>手机号或是密码输入,错误请重新输入.</script>');
            }else{
                //判断是否可以登录
                $this->load->model('user_model');
                $code = $this->user_model->login($phone, $pass, $data['auto'] ? 864000000 : $USER['setting']['loginexpire']);
                if($code == 1){
                    // 登录成功
                    $data['id'] = $this->uid;
                    $this->hooks->call_hook('user_login', $data); // 登录成功挂钩点
                    $success = $code;
                }else if($code == -1){
                    $error = fc_lang('用户不存在');
                }else if($code == -2){
                    $error = fc_lang('您的帐号或密码有误，请重新输入');
                }else if($code == -3){
                    $error = fc_lang('不是手机号码');
                }
            }
            if(IS_AJAX){
                $error && exit(dr_json(0, $error));
                $success && exit(json_encode(array(
                    'status' => 1,
                    'backurl' => $back_url,
                    'isdebug' => $_GET['debug'],
//                    'syncurl' => dr_member_sync_url($success)
                )));
            }


            //$success && $this->user_msg(fc_lang('登录成功').$success, $back_url, 1, $_GET['debug'] ? 99999 : 3);
            exit('error');
        }else{
            //exit;
            //$back_url = $this->input->get('back')?$this->input->get('back'):(isset($_SERVER['HTTP_REFERER']) ? (strpos($_SERVER['HTTP_REFERER'], 'login') !== false ? '' : $_SERVER['HTTP_REFERER']) : '');
        }
        $this->template->assign(array(
            'data'=>$data,
            'code' => $USER['setting']['logincode'],
            'back_url' =>$back_url,
            'meta_title'=>fc_lang('用户登录'),
            'result_error'=> $error
        ));
        $this->template->display('../gw/poup_login.html');
    }
}